<template>
  <div class="app-container">
    <div class="title-text">消息通知</div>
    <div class="btn" @click="del">一键清除已读</div>
    <div>
      <div class="card" v-for="i in this.notifyList">
        <div class="red-bot"></div>
        <div>
          <div style="font-weight: 400; font-size: 14px; color: #333333;">订单编号 {{ i.id }}</div>
          <div class="content">
            <span>{{i.name}} </span>
            <span class="read_blue" v-if="!i.read" @click="toRead(i.type)"> 点击去查看</span>
            <span class="read_gary" v-else @click="toRead(i.type)"> 点击去查看</span>
          </div>
        </div>
        <div class="time">{{ i.createTime }}</div>
      </div>
    </div>
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['messages:notify:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['messages:notify:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['messages:notify:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['messages:notify:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="notifyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="消息主键ID" align="center" prop="id" />
      <el-table-column label="消息的名称" align="center" prop="name" />
      <el-table-column label="消息类型 0商家余额不足 1商家下单背调订单  2商家下单批量订单  3候选人授权  4线上报告已出" align="center" prop="type" />
      <el-table-column label="消息对应数据的主键id" align="center" prop="dataId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['messages:notify:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['messages:notify:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

  </div>
</template>

<script>
import { listNotify, getNotify, delNotify, addNotify, updateNotify } from "@/api/messages/notify";
// let webSocket = new WebSocket("ws://localhost:8080/myWs1");
// const userid = this.$store.state.user.id;
// webSocket.onopen=function () {
//   webSocket.send("建立连接了")
// }
// webSocket.onmessage=function (msg) {

//   alert(msg.data)
// }
// webSocket.onclose=function () {
//   webSocket.send("我断开连接了")
// }

export default {
  name: "Notify",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 消息通知表格数据
      notifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询消息通知列表 */
    getList() {
      this.loading = true;
      listNotify(this.queryParams).then(response => {
        this.notifyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type: null,
        dataId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息通知";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNotify(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息通知";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateNotify(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotify(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除消息通知编号为"' + ids + '"的数据项？').then(function() {
        return delNotify(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('messages/notify/export', {
        ...this.queryParams
      }, `notify_${new Date().getTime()}.xlsx`)
    },
    // 去查看
    toRead(type) {
      console.log('type', type);
      // this.$router.push({ path: "/orderParent/ordersDetails", query: { id: row.id || this.ids } });
    },
    // 一键清除
    del() {
      for (let i = this.notifyList.length - 1; i >= 0; i--) {
        if (this.notifyList[i].read) {
          this.notifyList.splice(i, 1);
        }
      }
    },
  }
};
</script>
<style lang="scss" scoped>
  .title-text {
    width: 68px;
    height: 17px;
    margin: 0 0 36px 0;
    font-size: 16px;
    color: #333333;
  }
  .btn {
    // float: right;
    // justify-content: flex-end;
    position: absolute;
    right: 44px;
    width: 85px;
    height: 12px;
    font-size: 14px;
    color: #535E83;
    transform: translateY(-25px);
    cursor: pointer;
  }
  .card {
    height: 80px;
    background: #EFF2FF;
    border-radius: 5px;
    margin: 13px auto;
    display: flex;
    align-items: center;
    .red-bot {
      width: 10px;
      height: 10px;
      background: #FE2A47;
      border-radius: 50%;
      margin: 0 20px;
    }
    .content {
      margin-top: 12px;
      width: 677px;
      height: 13px;
      font-family: Microsoft YaHei;
      font-weight: 400;
      font-size: 12px;
      color: #666666;
      .read_blue {
        color: blue;
        cursor: pointer;
      }
      .read_gray {
        color: gray;
      }
    }
    .time {
      margin-left: auto;
      margin-right: 30px;
      font-size: 14px;
      color: #666666;
      transform: translateY(-10px);
    }
  }
</style>
